
#include "Tasks.h"
#include "Base64.h"
#include "Config.h"

namespace smartvoice {

SessionTask::SessionTask() :
	m_start_time(0)
{
}

ITask* SessionTask::Allocator(IService*)
{
	return new SessionTask;
}

SessionTask* SessionTask::Clone()
{
	SessionTask* new_task = new SessionTask;
	new_task->m_start_time    = m_start_time;
	new_task->m_caller_number = m_caller_number;
	new_task->m_caller_name   = m_caller_name;
	new_task->m_callee_number = m_callee_number;
	new_task->m_callee_name   = m_callee_name;
	new_task->m_audio         = m_audio;
	new_task->m_audio_format  = m_audio_format;
	new_task->m_text          = m_text;
	new_task->m_index_info    = m_index_info;
	new_task->SetTaskMeta( GetTaskMeta()->Clone() );
	return new_task;
}

NEBULA_ERROR_CODE SessionTask::Serialize(std::ostream &os)
{
	KeyNode* root = NewConfig("session");
	root->SetValue("start-time",    m_start_time);
	root->SetValue("caller-number", m_caller_number.c_str());
	root->SetValue("caller-name",   m_caller_name.c_str());
	root->SetValue("callee-number", m_callee_number.c_str());
	root->SetValue("callee-name",   m_callee_name.c_str());
	root->SetValue("audio",         Base64::Encode(m_audio).c_str());
	root->SetValue("audio-format",  m_audio_format.c_str());
	root->SetValue("text",          m_text.c_str());
	root->SetValue("index-info",    m_index_info.c_str());
	os << root;
	CloseConfig(root);
	return NEBULA_ERR_SUCCESS;
}

NEBULA_ERROR_CODE SessionTask::UnSerialize(std::istream &is)
{
	KeyNode* root = NewConfig("session");
	is >> root;

	const char* tmp = 0;

	if (0 == (tmp = root->GetValue("start-time")))
		m_start_time = 0;
	else
		m_start_time = atoi(tmp);

	if (0 == (tmp = root->GetValue("caller-number")))
		m_caller_number.clear();
	else
		m_caller_number = tmp;

	if (0 == (tmp = root->GetValue("caller-name")))
		m_caller_name.clear();
	else
		m_caller_name = tmp;

	if (0 == (tmp = root->GetValue("callee-number")))
		m_callee_number.clear();
	else
		m_callee_number = tmp;

	if (0 == (tmp = root->GetValue("callee-name")))
		m_callee_name.clear();
	else
		m_callee_name = tmp;

	if (0 == (tmp = root->GetValue("audio")))
		m_audio.clear();
	else
		m_audio = Base64::Decode(tmp);

	if (0 == (tmp = root->GetValue("audio-format")))
		m_audio_format.clear();
	else
		m_audio_format = tmp;

	if (0 == (tmp = root->GetValue("text")))
		m_text.clear();
	else
		m_text = tmp;

	if (0 == (tmp = root->GetValue("index-info")))
		m_index_info.clear();
	else
		m_index_info = tmp;

	CloseConfig(root);
	return NEBULA_ERR_SUCCESS;
}

} // namespace smartvoice

